package com.autotest.nore.controller;

import com.autotest.nore.common.exception.TokenEmptyException;
import com.autotest.nore.domain.Domain;
import com.autotest.nore.dto.DomainDTO;
import com.autotest.nore.service.DomainService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/autotest/domain")
public class DomainController {

    @Autowired
    private DomainService domainService;

    @PostMapping("/save")
    public void saveDomain(@RequestBody Domain domain, HttpServletRequest request) {
        HttpSession session = request.getSession(false); // 获取会话，如果不存在则返回null
        if (session == null || session.getAttribute("userName") == null) {
            throw new TokenEmptyException("用户未登录，请重新登录");
        }
        String userName = session.getAttribute("userName").toString();
        domain.setUpdateUser(userName);
        domainService.saveDomain(domain);
    }

    @PostMapping("/delete")
    public void deleteEnvironment(@RequestBody Domain domain) {
        domainService.deleteDomain(domain);
    }

    @GetMapping("/list/{environmentId}")
    public List<DomainDTO> getDomainList(@PathVariable String environmentId) {
        return domainService.getDomainList(environmentId);
    }

}
